package com.br.aadb;

import org.andengine.engine.Engine;
import org.andengine.engine.FixedStepEngine;
import org.andengine.engine.LimitedFPSEngine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.BaseGameActivity;

import com.br.aadb.Managers.ResourcesManager;
import com.br.aadb.Managers.SceneManager;

import android.view.Display;

public class GameActivity extends BaseGameActivity{

	//CONSTANTES
	public static final String JOGAR = "Jogar";
	public static final String ABOUT = "SOBRE";
	public static final String CONFIGURACOES = "OPÇÕES";
	
	//CONFIGURAÇÕES 
	private Camera mCamera;
	public int CAMERA_HEIGHT = 800;
	public int CAMERA_WIDTH = 480;	
	SceneManager sceneManager;
	private ResourcesManager resourcesManager;
	
	@Override
	public EngineOptions onCreateEngineOptions() {

		Display displayPadrao = getWindowManager().getDefaultDisplay();
		
		CAMERA_HEIGHT = displayPadrao.getHeight();
		CAMERA_WIDTH = displayPadrao.getWidth();
		//AJUSTANDO A CAMERA AO TAMANHO DO APARELHO
		mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

		EngineOptions options = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
		options.getAudioOptions().setNeedsMusic(true).setNeedsSound(true);
		options.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
		
		return options;
	}

	@Override
	public Engine onCreateEngine(EngineOptions pEngineOptions) {
		return new LimitedFPSEngine(pEngineOptions, 60);
	}

	@Override
	public void onCreateResources(
			OnCreateResourcesCallback pOnCreateResourcesCallback)
			throws Exception {
		
		ResourcesManager.prepareManager(mEngine, this, mCamera, getVertexBufferObjectManager());
		resourcesManager = ResourcesManager.getInstance();
		pOnCreateResourcesCallback.onCreateResourcesFinished();
		
	}

	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws Exception {
		
	   SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback);		
		
	}

	@Override
	public void onPopulateScene(Scene pScene,
			OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
		
		mEngine.registerUpdateHandler(new TimerHandler(3f, new ITimerCallback() 
	    {
	            public void onTimePassed(final TimerHandler pTimerHandler) 
	            {
	                mEngine.unregisterUpdateHandler(pTimerHandler);
	                SceneManager.getInstance().createMenuScene();
	            }
	    }));
    pOnPopulateSceneCallback.onPopulateSceneFinished();
    
	}

}
